#include<iostream>
#include<string>
#include<set>

using namespace std;

const int N = 16;
const int M = 100;
string ans;
string ls[M];
int t[M], c[M], cnt, chosen[M][N], wrong[N][300];
set<char> se[N];

void dfs(int m, int n) {
    if (m < 1) {
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < N; j++) {
                cout << chosen[i][j] << ' ';
            }
            cout << endl;
        }
        cout << ans << endl;
        cout << m << ' ' << n << endl;
    }

    if (m == cnt) {
        cout << ans << endl;
        return;
    }

    if (c[m] == 0) {
        //update wrong
        for (int i = 0; i < N; i++) {
            if (chosen[m][i]) {
                for (int ch = '0'; ch <= '9'; ch++) {
                    if (ch != ls[m][i]) {
                        wrong[i][ch]++;
                    }
                }
            } else {
                wrong[i][ls[m][i]]++;
            }
        }
        dfs(m + 1, 0);
        //unupdate wrong
        for (int i = 0; i < N; i++) {
            if (chosen[m][i]) {
                for (int ch = '0'; ch <= '9'; ch++) {
                    if (ch != ls[m][i]) {
                        wrong[i][ch]--;
                    }
                }
            } else {
                wrong[i][ls[m][i]]--;
            }
        }
        return;
    }

    if (n == N) {
        return;
    }

    if (wrong[n][ls[m][n]]) {
        dfs(m, n+1);
    } else {
        if (ans[n] == ls[m][n]) {
            chosen[m][n] = 1;
            c[m]--;
            dfs(m, n+1);
            chosen[m][n] = 0;
            c[m]++;
        } else {
            chosen[m][n] = 1;
            c[m]--;
            ans[n] = ls[m][n];
            dfs(m, n+1);
            chosen[m][n] = 0;
            c[m]++;
            ans[n] = '*';
            dfs(m, n+1);
        }
    }


}

int main()
{
    string s;
    int correct;

    ans = "";
    for (int i = 0; i < N; i++) ans += '*';

    while (cin >> s >> correct) {
        ls[cnt] = s;
        c[cnt] = correct;
        ++cnt;

        for (int i = 0; i < N; i++) {
            se[i].insert(s[i]);
        }
    }

    //first run dfs(0, 0) to the the ans, but the ans is missing a number in the 13th place
    //so run the snippet below to get the proper answer, or you can find it out by hand
    //dfs(0, 0);

    ans = "464026157184*533";
    for (char ch = '0'; ch <= '9'; ch++) {
        ans[12] = ch;
        bool good = true;
        for (int i = 0; i < cnt; i++) {
            int count = 0;
            for (int j = 0; j < N; j++) {
                if (ans[j] == ls[i][j])
                    count++;
            }
            if (count != c[i]) {
                good = false;
                break;
            }
        }

        if (good) cout << ans << endl;
    }

    return 0;
}
